package com.example.springmvcdemo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController // 告诉Spring这下面有东西需要看一下
@RequestMapping("/header")
public class HeaderController {
    // 获取cookie
    // servlet的实现方式: HTTP请求中所有的信息都在HttpServletRequest这个对象中,包括cookie
    // 对应的,所有相应信息都在HttpServletResponse这个对象中
    // HttpServletRequest和HttpServletResponse是Spring的内置对象,随去随用
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        // cookie可能为空,最好做i写空指针的判断
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        } else {
            return "cookie为空,获取失败";
        }
        return "获取cookie成功";
    }
    // Spring对servlet进行了封装
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("zhangsan") String key) {
        return "从cookie中获取信息,张三: "+key;
    }

    // 设置session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();// 先创建一个session对象,若没有则创建一个
        session.setAttribute("userName","zhangsan");
        session.setAttribute("age",16);
        return "设置session成功,userName: "+session.getAttribute("userName")+
                ",age: "+session.getAttribute("age");
    }

    // 获取session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession();// 先从cookie中拿到sessionId,再根据sessionId获取到session
        // session是键值对结构的,getAttribute可以拿到对应key的值
        String userName=(String)session.getAttribute("userName");
        if(userName!=null) {
            return "从session中获取信息,userName: " + userName;
        } else {
            return "userName为空!";
        }
    }

    // 第二张种获取session的方式,通过 HttpSession 获取session对象
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
        String userName=(String)session.getAttribute("userName");
        if(userName!=null) {
            return "从session2中获取信息,userName: " + userName;
        } else {
            return "userName为空!";
        }
    }

    // 第三种获取session的方式->注解
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("userName") String userName) {
        return "从session3中获取到session信息,userName: "+userName;
    }

    // 获取header
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:"+userAgent;
    }

    // 通过注解获取header
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent:"+userAgent;
    }
}

















